package com.jyb.qiyefuwu.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.jyb.qiyefuwu.api.EnterpriseServiceAPIManager;
import com.jyb.qiyefuwu.entity.FollowServiceEntity;
import com.sgb.net.entity.BaseEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ServiceFollowListViewModel extends BaseViewModel {


    private int page = 1;
    public boolean loadmore = false;
    public MutableLiveData<List<FollowServiceEntity>> listdata = new MutableLiveData<>();

    public ServiceFollowListViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getList(boolean loadmore){

        this.loadmore = loadmore;
        if(loadmore){
            page++;
        }else{
            page = 1;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("page",1);
        map.put("limit",20);
        map.put("attentionNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        mLoading.postValue(BaseViewModel.SHOW_LODING);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().getServiceList(map).enqueue(new Callback<BaseEntity<ListResponse<List<FollowServiceEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<FollowServiceEntity>>>> call, Response<BaseEntity<ListResponse<List<FollowServiceEntity>>>> response) {
                mLoading.postValue(BaseViewModel.DISMISS_LODING);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    listdata.postValue(response.body().getData().getList());
                } else {
                    mToast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<FollowServiceEntity>>>> call, Throwable t) {
                mLoading.postValue(BaseViewModel.DISMISS_LODING);
                mToast.postValue(t.getMessage());
            }
        });


    }


    public void updateFollowType(FollowServiceEntity entity){
        Map<String,Object> map = new HashMap<>();
        map.put("id",entity.getId());
        map.put("dataId", entity.getDataId());
        map.put("type", 7);
        map.put("attentionNot", 1);
        map.put("attentionTime", System.currentTimeMillis());
        map.put("attentionNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        mLoading.postValue(BaseViewModel.SHOW_LODING);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().saveAttention(map).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    mToast.postValue("已取消关注");
                    getList(false);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                mLoading.postValue(BaseViewModel.DISMISS_LODING);
                mToast.postValue(t.getMessage());
            }
        });
    }



}
